#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    labelPtr = ui->label;
    labelPtr->setText(QString());
    viewPtr = ui->graphicsView_Main;
    viewPtr->setScene(&mainScene);
    viewPtr->setDragMode(QGraphicsView::RubberBandDrag);
    viewPtr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    viewPtr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setAction();

    initLib(0);
}

MainWindow::~MainWindow()
{
    if (processTheard.isRunning())
    {
        processTheard.terminate();
    }
    releaseCapture();
    releaseLib();
    delete ui;
}

void MainWindow::on_pushButton_FrameRate_clicked()
{
    mainScene.resetROI();
    testMode = Video_File_Mode|Frame_Rate_Mode;
    openFile(true,testMode);
}

void MainWindow::on_pushButton_ColorTest_clicked()
{
    mainScene.resetROI();
    testMode = Image_File_Mode|Color_Check_Mode;
    openFile(false,testMode);
}

void MainWindow::on_pushButton_SFRTest_clicked()
{
    mainScene.resetROI();
    testMode = Image_File_Mode|SFR_Mode;
    openFile(false,testMode);
}

void MainWindow::on_pushButton_StepChartTest_clicked()
{
    mainScene.resetROI();
    testMode = Image_File_Mode|Stepchart_Mode;
    openFile(false,testMode);
}

bool MainWindow::openFile(bool videoFlag, int mode)
{
    QString fileName;
    double fps;
    int returnImageWidth;
    int returnImageHeight;
    int returnImageStep;
    unsigned char* data=NULL;
    bool getImageFlag;
    void* userdata = NULL;
    int timeStamp;
    bool openFlag;
    if (videoFlag)
    {
        fileName = QFileDialog::getOpenFileName(NULL,"open","./","Video (*.avi *.mpg)");
        if (fileName.size()<=0)
        {
            return false;
        }
        openFlag = openVideo(mode,(char *)fileName.toStdString().c_str(),0,&fps);
    }
    else
    {
        fileName = QFileDialog::getOpenFileName(NULL,"open","./","Images (*.png *.xpm *.jpg *.bmp)");
        if (fileName.size()<=0)
        {
            return false;
        }
        openFlag = openVideo(mode,(char *)fileName.toStdString().c_str(),0,&fps);
    }

    if (openFlag)
    {
        data = getOriginalData(&returnImageWidth,&returnImageHeight,&returnImageStep,&timeStamp,&getImageFlag,userdata);
        if (getImageFlag)
        {
            QImage tempImage(data,returnImageWidth,returnImageHeight,returnImageStep,QImage::Format_RGB888);
            imageWidth = returnImageWidth;
            imageHeight = returnImageHeight;
            emit sendImage(tempImage);
            ui->graphicsView_Main->fitInView(QRectF(-returnImageWidth/2,-returnImageHeight/2,returnImageWidth,returnImageHeight));
        }
        return true;
    }
    else
    {
        return false;
    }
    return false;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    //mainScene.itemsBoundingRect()
    ui->graphicsView_Main->fitInView(mainScene.itemsBoundingRect());
    QWidget::paintEvent(event);
}

bool MainWindow::setAction()
{
    processThreadClass.connect(this,SIGNAL(beginProcessSig()),&processThreadClass,SLOT(processSlot()));
    connect(&processThreadClass,SIGNAL(sendImage(QImage)),&mainScene,SLOT(updateImage(QImage)));
    connect(this,SIGNAL(sendImage(QImage)),&mainScene,SLOT(updateImage(QImage)));
    connect(&processThreadClass,SIGNAL(videoEnd()),this,SLOT(videoEndSlot()));
    processThreadClass.moveToThread(&processTheard);
    processTheard.start();
    return true;
}

void MainWindow::videoEndSlot()
{
    mainScene.resetROI();
    int resultList[2];
    getResult(resultList);
    char processMessage[200];
    getMessage(processMessage);
    labelPtr->setText(QString(processMessage));
    //QMessageBox::about(NULL,QString("Frame Rate Test"),QString(processMessage));
    emit showResultSig();
}

void MainWindow::on_pushButton_Comfirm_clicked()
{
    if (mainScene.isSetROI())
    {
        QRectF ROIRect = mainScene.getROI();
        bool setFlag = setROI((int)ROIRect.x(), (int)ROIRect.y(),
                              (int)ROIRect.width(), (int)ROIRect.height());
        if (setFlag)
        {
            if (testMode&Video_File_Mode)
            {
                emit beginProcessSig();
            }
            else
            {
                int returnImageWidth;
                int returnImageHeight;
                int returnImageStep;
                unsigned char* data=NULL;
                data = processFrame(&returnImageWidth, &returnImageHeight, &returnImageStep);

                QImage tempImage(data,returnImageWidth,returnImageHeight,returnImageStep,QImage::Format_RGB888);
                emit sendImage(tempImage);
                videoEndSlot();
                //processThreadClass.processImageFile();
            }
        }
        else
        {
            QMessageBox::critical(NULL,QString("Frame Rate Test"),QString("ROI or Test Methods Illegal!"));
        }
    }
    else
    {
        QMessageBox::critical(NULL,QString("Frame Rate Test"),QString("Set ROI First!"));
    }
}

void MainWindow::on_pushButton_Exit_clicked()
{
    exit(-1);
}

void MainWindow::on_pushButton_UserGuide_clicked()
{

}


void MainWindow::on_pushButton_EmailAuthor_clicked()
{

}

void MainWindow::on_pushButton_ResetROI_clicked()
{
    mainScene.resetROI();
}

void MainWindow::on_pushButton_CancelROI_clicked()
{
    mainScene.resetROI();
}

